﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
    public class Advert : Sprite
    {
        // CONSTRUCTEURS
        public Advert()
        {
        }

        // Simple Advert
        public Advert(Vector2 position, SpriteFont font, string text, float sectime, Vector2 direction, float speed, Color color, bool Scroll, bool centered)
        {
            this.Position = position;
            this.Font = font;
            this.Text = text;
            this.Age = (int)(60 * sectime);
            this.LifeTime = this.Age;
            this.Direction = direction;
            this.Speed = speed;
            this.Fading = 255f;
            this.SpriteColor = new Color(color.R, color.G, color.B, this.Fade);
            this.IsScrolled = Scroll;
            this.IsScaled = false;
            this.scale = 1;
            this.Centered = centered;
        }

        // Scaled Advert
        public Advert(Vector2 position, SpriteFont font, string text, float sectime, Vector2 direction, float speed, Color color, bool Scroll, Vector2 scale, bool centered)
        {
            this.Position = position;
            this.Font = font;
            this.Text = text;
            this.Age = (int)(60 * sectime);
            this.LifeTime = this.Age;
            this.Direction = direction;
            this.Speed = speed;
            this.Fading = 255f;
            this.SpriteColor = new Color(color.R, color.G, color.B, this.Fade);
            this.IsScrolled = Scroll;
            this.startscale = scale.X;
            this.endscale = scale.Y;
            this.IsScaled = true;
            this.scale = this.startscale;
            this.Centered = centered;
        }

        // CHAMPS
        private Vector2 Direction;
        public int LifeTime;
        public int Age;
        private float Speed;
        private string Text;
        public SpriteFont Font;
        private bool IsScrolled;
        private float Fading;
        private float startscale;
        private float endscale;
        private float scale;
        private bool IsScaled;
        private bool Centered;

        public void Update()
        {
            // Deplacement
            this.Position = this.Position + (Speed * this.Direction);

            // Duree de vie
            this.LifeTime--;

            // Fade
            this.Fading = (float)this.Fading - (255f / (float)this.Age);
            this.Fade = (byte)this.Fading;

            // Scale
            if (this.IsScaled)
            {
                this.scale = this.scale + (this.endscale / (float)this.Age);
            }
        }

        public override void Draw(SpriteBatch spriteBatch, Camera camera)
        {
            int c = 0;
            if (camera != null && IsScrolled)
                c = camera.Position;

            float xoffset = 0;
            float yoffset = 0;

            if (this.Centered)
            {
                xoffset = Font.MeasureString(Text).X * scale / 2;
                yoffset = Font.MeasureString(Text).Y * scale / 2;
            }
            spriteBatch.DrawString(Font, Text, new Vector2(Position.X - c - xoffset, Position.Y - yoffset), new Color(SpriteColor.R - (255 - Fade), SpriteColor.G - (255 - Fade), SpriteColor.B - (255 - Fade), Fade), 0f, Vector2.Zero, this.scale, SpriteEffects.None, 0f);
        }
    }
}
